from datetime import datetime
import io
from pickle import DICT
import paddleocr
import json
import base64
import DateTime
from flask import Flask, request,jsonify  
import numpy as np
from PIL import Image
app=Flask(__name__)
app.config['JSON_AS_ASCII']=False
@app.route("/PaddleOCR/DetectText",methods=["POST"])
def OCR():
    if(request.data==""):
        return APIResult.Error("request data is null")
    t1= datetime.utcnow()
    data=json.loads(request.data)
    imgbyte=base64.b64decode(data)
    image=io.BytesIO(imgbyte)
    temp= Image.open(image)
    img=np.array(temp)[:,:,:3]
    info= ppocr.ocr(img)
    result=""
    for textblocks in info:
        result= result+textblocks[1][0]
    t2=datetime.utcnow()
    c=t2-t1
    print("time:",c);
    print(result);
    return jsonify(result)
def main():
    global ppocr;
    ppocr=paddleocr.PaddleOCR(use_det=True,use_gpu=False,enable_mkldnn=True,max_side_len=960,cpu_math_library_num_threads=6);
    app.run(debug=True,host="0.0.0.0",port=5000)
if __name__=="__main__":
    main();

